package esqeee.xieqing.com.huangrs.doAction.api;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import com.xieqing.codeutils.util.ActivityUtils;
import com.xieqing.codeutils.util.AppUtils;
import com.xieqing.codeutils.util.DeviceUtils;
import com.xieqing.codeutils.util.IntentUtils;
import com.xieqing.codeutils.util.StringUtils;
import com.xieqing.codeutils.util.Utils;
import com.yicu.yichujifa.GlobalContext;

import esqeee.xieqing.com.huangrs.bean.JSONBean;
import esqeee.xieqing.com.huangrs.doAction.Base;
import esqeee.xieqing.com.huangrs.library.RuntimeLog;
import esqeee.xieqing.com.huangrs.ui.OpsActivity;

public class System extends Base {
    public static Base intansce;

    public static Base getIntansce() {
        if (intansce == null) {
            intansce = new System();
        }
        return intansce;
    }

    @Override
    public boolean post(JSONBean param) {
        String arg = param.getString("arg");
        JSONBean var;
        switch (arg) {
            case "打开权限设置":
                GlobalContext.getContext().startActivity(new Intent(GlobalContext.getContext(), OpsActivity.class));
                break;
            case "取手机信息":
                var = checkVarExiets("var");
                setValue(var, DeviceUtils.getAllMessage());
                break;
            case "置屏幕亮度":
                int value = getMathResultOrThrow("value");
                Window localWindow = ActivityUtils.getTopActivity().getWindow();
                WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
                localLayoutParams.screenBrightness = ((float) value) / 255.0f;
                localWindow.setAttributes(localLayoutParams);
                break;
            case "取屏幕中心点":
                JSONBean x = checkVarExiets("x");
                JSONBean y = checkVarExiets("y");
                String offsetX = getStringFromParam("offsetX");
                if(StringUtils.isEmpty(offsetX)){
                    offsetX = "0";
                }
                String offsetY = getStringFromParam("offsetY");
                if(StringUtils.isEmpty(offsetY)){
                    offsetY = "0";
                }
                RuntimeLog.e("偏移量：" + offsetX + "," + offsetY);
                DisplayMetrics dm = new DisplayMetrics();
                Display defaultDisplay = ((WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                defaultDisplay.getMetrics(dm);
                int screenWidth = dm.widthPixels;
                int screenHeight = dm.heightPixels;
                setValue(x, (screenWidth / 2) + Integer.parseInt(offsetX));
                setValue(y, screenHeight / 2 + Integer.parseInt(offsetY));
                break;
            case "置屏幕亮度模式":
                int mode = param.getInt("mode", 0);
                android.provider.Settings.System.putInt(GlobalContext.getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
                break;
            case "调用系统分享":
                String text = getStringFromParam("text");
                String image = getStringFromParam("image");
                if (!TextUtils.isEmpty(image)) {
                    GlobalContext.getContext().startActivity(IntentUtils.getShareImageIntent(text, image, true));
                } else {
                    GlobalContext.getContext().startActivity(IntentUtils.getShareTextIntent(text, true));
                }
                break;
            case "调用系统打开文件":
                GlobalContext.getContext().startActivity(IntentUtils.getFileIntent(getStringFromParam("value")));
                break;

            case "卸载应用":
                AppUtils.uninstallApp(getStringFromParam("app"));
                break;
            case "取应用信息":
                var = checkVarExiets("var");
                setValue(var, AppUtils.getAppInfo(getStringFromParam("app")).toString());
                break;
            case "打开网络设置":
                GlobalContext.getContext().startActivity(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
                break;
        }
        return true;
    }


    @Override
    public int getType() {
        return 2;
    }

    @Override
    public String getName() {
        return "文件操作";
    }
}
